package it.intecs.pisa.smarcos.activities;

import it.intecs.pisa.smarcos.R;
import it.intecs.pisa.smarcos.base.BeepManager;
import it.intecs.pisa.smarcos.base.SpeechManager;

import java.io.IOException;
import java.util.List;
import java.util.Locale;

import android.content.Context;
import android.content.Intent;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.Vibrator;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class Position extends BaseActivity
{
	public static final String INTENT_SHOW="it.intecs.pisa.smarcos.activities.Position.SHOW";
	private static final String TAG = Position.class.getSimpleName();
	
	private View view; 
	private TextView textView;
	private ViewGroup baseCenterView;
	private String position;
	private String lastInformation;
	private Vibrator vibrator;
	private BeepManager beepManager;
	private SpeechManager speechManager;
	private Geocoder geocoder;
	private LocationManager locationManager;
	private GPSLocationListener locationListener;
    private String myPosition;
    private double latitude;
    private double longitude;		
 
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)    {
        super.onCreate(savedInstanceState);
        
        Log.d(TAG, "OnCreate");

		speechManager = SpeechManager.getInstance(getApplicationContext());
		beepManager = BeepManager.getInstance(getApplicationContext());
		vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);

		// Metodo di BaseViewManager
		Init(getBaseContext());
		inflater = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
		view = inflater.inflate(R.layout.generic_option, null);
		setContentView(getBaseView());
		baseCenterView = (ViewGroup) findViewById(R.id.center_base_view);
		baseCenterView.addView(view);	
		
		textView = (TextView) findViewById(R.id.Option);
		
		position = getString(R.string.TTS_Position);
			
		geocoder = new Geocoder(this, Locale.getDefault());
		
		// GPS
		locationListener = new GPSLocationListener();
		locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
		locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
	}
    
    @Override
    public void onDestroy(){
     	locationManager.removeUpdates(locationListener);
    	super.onDestroy();
    }
    
	// CLOSE
	@Override
	public void onClick_A(){
		Log.d(TAG, "onClick_A");
			
		Intent intent;
		intent=new Intent("it.intecs.pisa.smarcos.services.ResultReceiver.SHOW");
		intent.putExtra("Result", "NORESULT");
		intent.addCategory(Intent.CATEGORY_DEFAULT);
		sendBroadcast(intent);
		sentResult = true;
		finish();	
	}
	
	// POSITION
	@Override
	public void onClick_B(){
		Log.d(TAG, "onClick_B");
		
		speechManager.say(position, true, true);
	}
	
	// REPEAT 
	@Override
	public void onClick_C(){
		Log.d(TAG, "onClick_C");
		speechManager.say(lastInformation, true, true);
	}

	// BACK - DONE
	@Override
	public void onClick_D(){

		Log.d(TAG, "onClick_D");
		onClick_A();
	}
	
	
	private class GPSLocationListener implements LocationListener {

		private final String TAG = GPSLocationListener.class.getSimpleName();
		
		  public void onLocationChanged(Location arg0) {
		    latitude = arg0.getLatitude();
		    longitude = arg0.getLongitude();
		    
		    Log.d(TAG, "latitude " + latitude + " longitude " + longitude);
		    
		    myPosition();
		    textView.setText(lastInformation);
		    speechManager.say(lastInformation, true, false);
		  }

		  public void onProviderDisabled(String provider) {}

		  public void onProviderEnabled(String provider) {}
		  
		  public void onStatusChanged(String provider, int status, Bundle extras) {}
	}	

	
	List<Address> handleGeocoder(double lat, double lon){
		
		List<Address> addresses;
		
		try {
			addresses = geocoder.getFromLocation(lat, lon, 5);
			//addresses = geocoder.getFromLocation(43.656683,10.438218, 5);
			//addresses = geocoder.getFromLocation(43.655228,10.437493, 5);
		} catch (IOException e){
			Log.d(TAG, "The network is unavailable or an I/O problem occurs ");
			return null;
		} catch (IllegalArgumentException e){
			Log.d(TAG, "Latitude is less than -90 or greater than 90 or ");
			Log.d(TAG, "Longitude is less than -180 or greater than 180");
			return null;
		}
		return addresses;
	}
	
	
	private Boolean myPosition() {
		
		List<Address> addresses = handleGeocoder(latitude, longitude);

		if (addresses == null || addresses.isEmpty())
			myPosition = getString(R.string.TTS_NoStreet);
		else{
			myPosition = addresses.get(0).getAddressLine(0);
			Log.d(TAG, addresses.toString());
		}

		lastInformation = myPosition;
		Log.d(TAG, myPosition);
		return true;
	}
}

